﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using YiDa.Infrastructure;
using SYMetro.ServiceContract.Membership;
using SYMetro.DataObjects;

namespace SYMetro.Application.Test
{
    [TestClass]
    public class RoleServiceTest
    {
        [TestMethod]
        public void TestCreateAndDeleteRole()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            RoleDataObjectList roles = new RoleDataObjectList();
            RoleDataObject role = new RoleDataObject() { ID = Guid.NewGuid().ToString(), RoleName = "测试角色", Flag = RoleTypeEnum.Role };
            roles.Add(role);
            var list = service.CreateRoles(roles);
            IDList ids = new IDList();
            ids.Add(list[0].ID);
            service.DeleteRole(ids,true);
        }

        [TestMethod]
        public void TestIsUserInRole()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.IsUserInRole("用户01", "父角色01");
            Assert.IsTrue(result);
        }

        [TestMethod]
        public void TestGetRolesForUser()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.GetRolesForUser("用户01");
            Assert.AreEqual(3, result.Count);
        }

        [TestMethod]
        public void TestGetAllRoles()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.GetAllRoles();
            Assert.AreEqual(3, result.Count);
        }

        [TestMethod]
        public void TestRoleExists()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.RoleExists("父角色01");
            Assert.IsTrue(result);
        }

        [TestMethod]
        public void TestFindUsersInRole()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.FindUsersInRole("子角色01", "用户");
            Assert.AreEqual(2, result.Count);
        }

        [TestMethod]
        public void TestGetUsersInRole()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.GetUsersInRole("子角色01");
            Assert.AreEqual(2, result.Count);
        }

        [TestMethod]
        public void TestGetRolesByParentId()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var result = service.GetRolesByParentId(Guid.Empty);
            Assert.AreEqual(1, result.Count);
        }

        [TestMethod]
        public void TestFindUsersInRoleA()
        {
            var service = ServiceLocator.Instance.GetService<IRoleService>();
            var r = service.GetRoleByName("子角色01");
            Assert.IsNotNull(r);
            var o = service.FindUsersInRole(Guid.Parse(r.ID));
            Assert.AreEqual(2, o.Count);
        }
        

        //待测方法        
        //RemoveUsersFromRoles
        //AddUsersToRoles        
    }
}
